package com.xframe.xdal.config;

import com.xframe.xdal.core.DataBaseAccess;
import com.xframe.xdal.core.es.ESCilent;
import com.xframe.xdal.core.serialize.JsonConfigContainer;
import com.xframe.xdal.core.serialize.lang.LanguageEnvironmentContext;
import com.xframe.xdal.core.util.EncryptUtil;
import com.xframe.xdal.core.util.StringUtil;
import com.xframe.xdal.core.util.redis.RedisAccess;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;

import java.io.File;
import java.net.URL;
import java.util.List;

/**
 * 功能描述:spring ApplicationRunner 接口的实现，自动装载 配置，并实例化数据库连接池
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-26
 */
@Log4j2
@Order(value = 10)
@AutoConfiguration
public class XDALRunner implements ApplicationRunner {

    @Bean
    public DataSourceTypeEnumConvert getDataSourceTypeEnumConvert() {
        //log.info("dataSourceTypeEnumConvert()=================================>");
        return new DataSourceTypeEnumConvert();
    }
    @Bean
    public XDalConfigBean createConfigBean() {
        //log.info("XDALRunner.xDalConfigBean()=================================>");
        configBean = new XDalConfigBean();
        return configBean;
    }


    private XDalConfigBean configBean;

    @Override
    public void run(ApplicationArguments args) throws Exception {


        curXDALRunner = this;

        List<DataSourceConfig> dataSourceConfigList = configBean.getDataSourceConfigList();

        for (DataSourceConfig dataSourceConfig : dataSourceConfigList) {
            DataBaseAccess.registerDataSource(dataSourceConfig);
            log.info("XDal===================>>>>>>>>>>>>>>{}", dataSourceConfig);
        }

//        if(configBean.getRedis() != null ) {
//            RedisAccess.init(configBean.getRedis());
//        }
        if(configBean.getRedisConfigList() != null && configBean.getRedisConfigList().size() > 0){
            RedisAccess.init(configBean.getRedisConfigList());
        }
        if(configBean.getElasticsearch() != null) {
            ESCilent.registerDataSource(configBean.getElasticsearch());
//            ElasticsearchConfig.init(configBean.getElasticsearch());
        }
        EncryptUtil.setKeyAlgorithm(configBean.getEncryptionKey());
        if (!StringUtil.isEmpty(configBean.getSerJsonConfigPath())) {

            //ClassPathResource resource = new ClassPathResource(configBean.getSerJsonConfigPath());

            String pathbyJson;
            if(!StringUtil.isEmpty(configBean.getConfigfielBasePath())){
                pathbyJson = configBean.getConfigfielBasePath() + File.separator + configBean.getSerJsonConfigPath();
            }else {
                pathbyJson = Thread.currentThread().getContextClassLoader().getResource(configBean.getSerJsonConfigPath()).getPath();
            }
            log.info(pathbyJson);
            File file = new File(pathbyJson + File.separator);
            if (file.exists()) {
                JsonConfigContainer.init(pathbyJson + File.separator);
            }else {
                log.info("configBean.getSerJsonConfigPath() = {} path not exists",configBean.getSerJsonConfigPath());
            }

        }

        if (!StringUtil.isEmpty(configBean.getLangConfigPath())) {
            String pathbylang = null;

            if(!StringUtil.isEmpty(configBean.getConfigfielBasePath())){
                pathbylang = configBean.getConfigfielBasePath() + File.separator + configBean.getLangConfigPath();
            }else {
                URL url = Thread.currentThread().getContextClassLoader().getResource(configBean.getLangConfigPath());
                if(url != null){
                    pathbylang = url.getPath();
                }
                //pathbylang = Thread.currentThread().getContextClassLoader().getResource(configBean.getLangConfigPath()).getPath();
            }
            if(pathbylang != null) {
                log.info(pathbylang);
                File file = new File(pathbylang + File.separator);
                if (file.exists()) {
                    LanguageEnvironmentContext.init(pathbylang + File.separator);
                } else {
                    log.info("configBean.getConfigfielBasePath() = {} path not exists", configBean.getConfigfielBasePath());
                }
            }
        }
    }

    private static XDALRunner curXDALRunner = null;

    public static XDalConfigBean getConfigBean(){
        return curXDALRunner.configBean;
    }
}
